cmake_minimum_required(VERSION 3.0.0)

set(pro_name stack_clawer)

project(${pro_name} C CXX)

include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -std=c++17")
elseif(COMPILER_SUPPORTS_CXX0X)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
    message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third/curl_http)
# add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third/jsoncpp)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third/mysql_opt)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third/log)

set(SRC_FILES
    ${CMAKE_CURRENT_SOURCE_DIR}/stock_crawler.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/stock/sz_request.cpp
)
add_executable(${pro_name} ${SRC_FILES})
target_link_directories(${pro_name} PRIVATE
    ${CMAKE_CURRENT_BINARY_DIR}/third/curl_http
    ${CMAKE_CURRENT_BINARY_DIR}/lib
)
target_link_libraries(${pro_name} curl_http mysql_opt log)
target_include_directories(${pro_name} PRIVATE 
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/third
    # ${CMAKE_CURRENT_SOURCE_DIR}/third/jsoncpp/include
    ${CMAKE_CURRENT_SOURCE_DIR}/third/RapidJSON/include
    ${CMAKE_CURRENT_SOURCE_DIR}/third/log
)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/lib)
#
# Set RPATH so that libraries in run/ folder are found by the executable
# (this is default behavior on Windows).
#

set_property(TARGET ${pro_name} PROPERTY BUILD_WITH_INSTALL_RPATH ON)

set(rpath)
if(APPLE)
  set(rpath "@loader_path")
elseif(NOT WIN32)
  set(rpath "$ORIGIN")
endif()

if(rpath)
  set_property(TARGET ${pro_name}
    APPEND PROPERTY INSTALL_RPATH "${rpath}"
  )
endif()

file(GLOB libs "${CMAKE_CURRENT_BINARY_DIR}/lib/*${CMAKE_SHARED_LIBRARY_SUFFIX}*")
message("libs:${libs}")
file(INSTALL ${libs} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/bin)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/config.json DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/bin)

if(APPLE)
elseif(NOT WIN32)
file(INSTALL "/usr/local/lib/libcurl.so" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/bin)
endif()

# file(GLOG glibs "/usr/local/lib64/*stdc++.so*")
if(APPLE)
elseif(NOT WIN32)
set(glibs 
    "/usr/local/lib64/libstdc++.so.6.0.28"
    "/usr/local/lib64/libstdc++.so.6"
    "/usr/local/lib64/libstdc++.so")
file(INSTALL ${glibs} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/bin)
endif()